//
//  YQMYTabBarBaseViewController.m
//  MuYing
//
//  Created by tbb on 2019/7/18.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "YQMYTabBarBaseViewController.h"
#import "YQCustomTabBar.h"
#import "YQMYBaseNavigationController.h"
#import "MYHomeVC.h"
#import "MYShopVC.h"
#import "MYPublishVC.h"
#import "MYNewsVC.h"
#import "MYMyVC.h"
#import "YQMYBaseNavigationController.h"
#import "MYLoginVC.h"
@interface YQMYTabBarBaseViewController ()<UITabBarControllerDelegate>

@end

@implementation YQMYTabBarBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 利用KVO来使用自定义的tabBar
    YQCustomTabBar *customTabBar = [[YQCustomTabBar alloc] init];
    [self setValue:customTabBar forKey:@"tabBar"];
    [customTabBar.centerBtn addTarget:self action:@selector(centerAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBar.backgroundColor = [UIColor whiteColor];
    self.delegate = self;
    
    //去除tabBar顶部横线的方法
    //self.tabBar.clipsToBounds = YES;
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10 weight:(UIFontWeightSemibold)];
    normalAttrs[NSForegroundColorAttributeName] = kAPPThreeTitleColor;
    
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10 weight:(UIFontWeightSemibold)];
    selectedAttrs[NSForegroundColorAttributeName] = kAPPThemeColor;
    
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    self.tabBar.tintColor = kAPPThemeColor;
    // 设置背影色
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
    
    //移除顶部线条
    self.tabBar.backgroundImage = [UIImage new];
    self.tabBar.shadowImage = [UIImage new];
    //添加阴影
    self.tabBar.layer.shadowColor = HexColor(0x000000).CGColor;//[UIColor lightGrayColor].CGColor;
    self.tabBar.layer.shadowOffset = CGSizeMake(0, -2);
    self.tabBar.layer.shadowOpacity = 0.08;
    
    [self addChild:[[MYHomeVC alloc] init] title:kYQLS(@"社区") nor:@"MY_tabBar_Home_d_icon" sel:@"MY_tabBar_Home_s_icon"];
    [self addChild:[[MYShopVC alloc] init] title:kYQLS(@"商店") nor:@"MY_tabBar_Shop_d_icon" sel:@"MY_tabBar_Shop_s_icon"];
    
//    [self addChild:[[MYPublishVC alloc] init] title:@"" nor:@"MY_tabBar_Publish_s_icon" sel:@"MY_tabBar_Publish_s_icon"];

    [self addChild:[[MYNewsVC alloc] init] title:kYQLS(@"资讯") nor:@"MY_tabBar_News_d_icon" sel:@"MY_tabBar_News_s_icon"];
    [self addChild:[[MYMyVC alloc] init] title:kYQLS(@"我的") nor:@"MY_tabBar_my_d_icon" sel:@"MY_tabBar_my_s_icon"];
    
}

-(void)addChild:(UIViewController*)vc title:(NSString*)title nor:(NSString*)nor sel:(NSString*)sel{
    
    YQMYBaseNavigationController * nav = [[YQMYBaseNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    
    nav.tabBarItem.title = title;
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:sel] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.image = [[UIImage imageNamed:nor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    if ([title isEqualToString:@""]) {
        nav.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
        return;
    }
    //调整文字和底边的距离
    [nav.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    
}


#pragma mark UITabBarControllerDelegate

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
}
#pragma mark UITabBarControllerDelegate end
-(void)centerAction:(UIButton *)sender {
    
    
    NSString * token = [NSString stringWithFormat:@"%@",[YQPUserTool account].token];
    NSLog(@"token--- %zd",[token removeStringSpace].length);
    if ([token removeStringSpace].length > 10) {
        MYPublishVC *publishVC = [[MYPublishVC alloc] init];
        YQMYBaseNavigationController *navVC = [[YQMYBaseNavigationController alloc] initWithRootViewController:publishVC];
        navVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:navVC animated:true completion:nil];
    }else{
        QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:kYQLS(@"发布动态需要登陆") message:@"" preferredStyle:(QMUIAlertControllerStyleAlert)];
        
                [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"取消") style:(QMUIAlertActionStyleCancel) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
                }]];
                alert.alertCancelButtonAttributes = @{NSForegroundColorAttributeName:HexColor(0xB7B7B7),NSFontAttributeName:kSystemMedium(17),NSKernAttributeName:@(0)};
                
                [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"去登陆") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
                    MYLoginVC *loginVc = [[MYLoginVC alloc] init];
                    YQMYBaseNavigationController *navVC = [[YQMYBaseNavigationController alloc] initWithRootViewController:loginVc];
                    [UIApplication sharedApplication].keyWindow.rootViewController = navVC;
                }]];
            alert.alertButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:kSystemMedium(17),NSKernAttributeName:@(0)};
            [alert showWithAnimated:YES];
    }

}

- (void)tabBarSetSelectedIndex:(NSInteger)index {
    [self setSelectedIndex:index];
}


@end
